﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using QQ.Util;
using System.Threading;
using RuoTeng.Util;
using System.Windows.Forms;

namespace RTGather.Lib.gather
{

    /// <summary>
    ///雅虎台湾购物网站采集
    ///
    /// </summary>
   public class fbb_bugyahoo
    {
        
        private string countpattern = @"(?isx)<strong\sclass='count'>(?<count>.*?)</strong>";//总条数 
        //================================================================ 

        private string areapattern = @"(?isx)<li\sclass=.cfx.>(?<inner>.*?)</li>";//块

        private string Pidpattern = @"(?isx)gdid=(?<pid>\d{1,})";//Pid 

        private string descpattern = @"(?isx)<p\sclass='desc'>(?<desc>.*?)<br></p>";//描述

        private string titlePattern = @"(?isx)class='name'>(?<title>.*?)</a>";//标题 

        private string tuijianPattern = @"(?isx)<!--特別推薦\s-->(?<tuijian>.*?)<!--特別推薦\sEND-->";//推荐 

        private static string rootpath = Application.StartupPath;//根目录

        private string oldgather = rootpath + "\\已采集产品.txt";

        private List<Product> clist = new List<Product>();

        public void getData(string url,string code,string format)
        {

            string html=Http.GetData(url, null,null,Encoding.UTF8);
                       
            MatchCollection matches = Regex.Matches(html, areapattern);
            clist.Clear();
            
            foreach (Match match in matches)
            {
                string innerurl = "";
                GroupCollection groups = match.Groups;

                if (groups.Count > 0)
                {

                    innerurl = groups["inner"].ToString();  
                    string pname = getStrFromPattern(titlePattern, innerurl, "title"); 
                    string pid = getStrFromPattern(Pidpattern, innerurl, "pid");
                    string desc = getStrFromPattern(descpattern, innerurl, "desc");

                 
                    Product p = new Product();
                  
                    if (IsRepeat(pid, oldgather) == false)
                    {
                        p.Name = pname;
                        p.Pid = pid;
                        p.vcode = makeCode(code, pid, pname, format);
                       //采集特别推荐
                       string innerurl2 = string.Format("http://buy.yahoo.com.tw/gdsale/gdsale.asp?gdid={0}", pid);
                       string innerhtml = Http.GetData(innerurl2, null,null,Encoding.UTF8);

                        string tuijian = getStrFromPattern(tuijianPattern, innerhtml, "tuijian");
                     tuijian = NoHTML(tuijian);

                        p.desc = desc + "<br>"+ tuijian;
                        if (desc == "没有登记")
                        {
                            p.desc = pname + "<br>"+ tuijian;
                        }  

                        //结束
                        //写入文件
                        writeNew(pid, oldgather);
                        clist.Add(p);
                    } 
                }

            }
        }



        public string NoHTML(string Htmlstring)
        {
            //删除脚本
            Htmlstring = Htmlstring.Replace("\r\n", "");
            Htmlstring = Regex.Replace(Htmlstring, @"<script.*?</script>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<style.*?</style>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<.*?>", "", RegexOptions.IgnoreCase);
            //删除HTML
            Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
            Htmlstring = Htmlstring.Replace("<", "");
            Htmlstring = Htmlstring.Replace(">", "");
            Htmlstring = Htmlstring.Replace("\r\n", "");

            return Htmlstring;
        }



        /// <summary>
        /// 判断是否重复
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        private bool IsRepeat(string url, string filename)
        {
            List<string> list = TextBoxTools.getFileList(filename);
            bool isrepeat = list.Contains(url);
            return isrepeat;
        }


        /// <summary>
        /// 写入新的记录
        /// </summary>
        /// <param name="pid"></param>
        /// <param name="filename"></param>
        private void writeNew(string pid, string filename)
        {
            string old = RTFile.ReadFile(filename);
            string newstring = old + pid + System.Environment.NewLine;
            RTFile.WriteFile(filename, newstring);

        }
         
        public string getStrFromPattern(string pattern,string content,string name)
        {
            Match match = Regex.Match(content, pattern);
            string result="";
            if (match.Success)
            {
                GroupCollection groups = match.Groups;
                result = groups[name].ToString();
            }
            else
            {
                result = "没有登记";
            }
            return result; 
        }



        public string makeCode(string code, string pid, string title,string format)
        {

            string result = string.Format("<A href=\"http://tw.partner.buy.yahoo.com/gd/buy?gdid={0}&amp;mcode={1}\" target=_blank><IMG border=0 alt=\"{2}\" src=\"http://tw.ptnr.yimg.com/no/gd/img?gdid={0}&amp;fc=blue&amp;s=30&amp;vec={3}\"></A> ", pid, code, title, format);

            return result;
        }

        /// <summary>
        /// 根据关键词搜索提取条数
        /// </summary>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public int getCount(string keyword)
        {
            string url = string.Format("http://search.buy.yahoo.com.tw/search.php?ei=utf8&hpp=gdsearch&kw={0}&catsel=&z=0&subno=0&from=", System.Web.HttpUtility.UrlEncode(keyword).ToUpper());

            string result = Http.GetData(url, null,null,Encoding.UTF8);

            string count = getStrFromPattern(countpattern, result, "count");
            int num = 0;
            if (count != "没有登记")
            {
                num = ParseType.ParseInt(count, 0);
            } 
            return num;
        }

        /// <summary>
        /// 计算页数 
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        public int getPageCount(int count)
        {

            int PageSize = 20;

            int pagenum = (count / PageSize) + 1;

            return pagenum;

        }


        /// <summary>
        /// 导出 文件excel
        /// </summary>
        /// <param name="filename"></param>
        public void ExcelExport(string filename)
        {
            Excel.Application xls_app = null;
            Excel._Workbook xls_book = null;
            Excel._Worksheet xls_sheet = null;
            System.Reflection.Missing miss = System.Reflection.Missing.Value;
            try
            {
                xls_app = new Excel.ApplicationClass();
                xls_app.Visible = false;
                Excel.Workbooks workBooks = (Excel.Workbooks)xls_app.Workbooks;
                if (!System.IO.File.Exists(filename))
                {
                    xls_book = (Excel.Workbook)workBooks.Add(true);
                    Excel.Sheets _sheets = xls_book.Sheets;
                    xls_sheet = (Excel.Worksheet)_sheets[1];
                    int columnIndex = 1;

                    xls_sheet.Cells[1, columnIndex] = "产品编号"; columnIndex++; 
                    xls_sheet.Cells[1, columnIndex] = "产品标题"; columnIndex++; 
                   xls_sheet.Cells[1, columnIndex] = "产品描述"; columnIndex++;  
                    xls_sheet.Cells[1, columnIndex] = "产品代码"; columnIndex++; 
                   
                   
                    xls_book.SaveAs(filename, miss, miss, miss, miss, miss, Excel.XlSaveAsAccessMode.xlNoChange, miss, miss, miss, miss, miss);
                    xls_book.Close(false, miss, miss);

                }
                xls_book = workBooks.Open(filename, miss, miss, miss, miss, miss, Excel.XlSaveAsAccessMode.xlNoChange, miss, miss, miss, miss, miss);
                Excel.Sheets sheets = xls_book.Sheets;
                xls_sheet = (Excel.Worksheet)sheets[1];

                int rowIndex = xls_sheet.UsedRange.Rows.Count == 0 ? 1 : xls_sheet.UsedRange.Rows.Count + 1;

                foreach (Product c in this.clist)
                {

                    int columnIndex = 1;

                    xls_sheet.Cells[rowIndex, columnIndex] = c.Pid;
                    columnIndex++;


                    xls_sheet.Cells[rowIndex, columnIndex] = c.Name;
                    columnIndex++;

                    xls_sheet.Cells[rowIndex, columnIndex] = c.desc;
                    columnIndex++;

                    xls_sheet.Cells[rowIndex, columnIndex] = c.vcode;
                    columnIndex++;

                    rowIndex++;

                }
                xls_book.Save();
                xls_book.Close(false, miss, miss);
                xls_app.Quit();
            }
            catch(Exception ex) { if (xls_book != null) { xls_book.Save(); xls_book.Close(false, miss, miss); xls_app.Quit(); } }
        }


    }
}
